<?php

    class Vesti_m extends Model {

        function Vesti_m () {
            parent::Model();
            $this->load->library('form_validation');             
        }

        public function validate_vest()
        {
            $this->form_validation->set_rules('title','naslov vijesti','required|trim');
            $this->form_validation->set_rules('text','tekst vijesti','required|trim');


            $this->form_validation->set_message('required', 'Molimo unesite %s.');


            $this->form_validation->set_error_delimiters('<div class="ValidationErrors">', '</div>');

            return $this->form_validation->run();
        }

        public function createavest()  /* return:TRUE or FALSE */
        {  

            if($this->validate_vest()){
                /*STATUS OPTIONS*/
                $status = 1;

                if(!isset($_POST['status'])){
                    $status = 2;
                }

                /*CREATE VEST*/
                $vest = array(
                'status' => $status,
                'account_id' => $this->session->userdata('id'),
                'foto_path' => $_POST['_foto'],
                'created' => time()
                );
                

                $this->db->insert('news', $vest);  
                $vestid = $this->db->insert_id();

                /*CREATE A CONTENT*/
                $content = array(
                'title' => $_POST['title'],
                'text' => $_POST['text'],
                'news_id' => $vestid,
                'lang_id' => 'en'
                );
                
                $this->db->insert('content', $content);    //CREATE TITLE AND TEXT                       


                return 1;
            }else{
                return FALSE;
            }
        }

        public function listaj_vesti($ac_id=NULL,$condition = NULL){
            if($ac_id==NULL) $ac_id = $this->session->userdata('id');
            if($condition==NULL) $condition = array('account_id'=> $ac_id,'status !='=>0);
            $this->db->select('news.*'); 
            $this->db->select('content.title'); 
            $this->db->select('content.text'); 
            $this->db->select('content.lang_id'); 
            $this->db->from('news', 'content');
            $this->db->where($condition);
            $this->db->join('content', 'content.news_id = news.id');
            $this->db->order_by('news.id','desc');

            $res = $this->db->get()->result();
            //$this->firephp->fb('last:'.$this->db->last_query()); 
            return $res; 

        }  
        public function editvest()
        {
            if($this->validate_vest()){
                /*STATUS OPTIONS*/
                $status = 2;

                if(isset($_POST['status'])){
                    $status = 1;
                }

                /*EDIT VEST*/
                $vest = array(
                'status' => $status,
                'account_id' => $this->session->userdata('id'),
                'foto_path' => $_POST['_foto']
                );

                $this->db->where('id', $_POST['vest_id'])->update('news', $vest); 

                /*CREATE A CONTENT*/
                $content = array(
                'title' => $_POST['title'],
                'text' => $_POST['text'],
                'news_id' => $_POST['vest_id'],
                'lang_id' => 'en'
                );
                
                $this->db->where('news_id', $_POST['vest_id'])->update('content', $content);    //UPDATE TITLE AND TEXT                       

                return 1;
            }else{
                return FALSE;
            }
        }

        public function citaj_vest($id=NULL){ /* return:object (title, description, keywords) */
            if($id!=NULL){
                if(isset($_GET['id'])){
                    $id=$_GET['id'];
                }  
            }
            $this->db->select('news.*'); 
            $this->db->select('content.title'); 
            $this->db->select('content.text'); 
            $this->db->select('content.lang_id'); 
            $this->db->from('news', 'content');
            $this->db->where( array('news.id'=> $id));
            $this->db->join('content', 'content.news_id = news.id');

            $res = $this->db->get()->result();
            //$this->firephp->fb('last:'.$this->db->last_query()); 
            return $res;
        }
        public function vest_status($id,$status){
            return $this->db->where('id', $id)->update('news', array('status'=>$status));
        }

    }
?>
